<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="数学建模算法6-灰色关联分析与预测, python,machine learning,deep learning,html,css,c,c++,cpp,cmake,ros,linux,ubuntu">
    <meta name="description" content="本文主要介绍了数学建模中常见的分析与预测算法：灰色关联分析和灰色预测">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>数学建模算法6-灰色关联分析与预测 | JackWang&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.2">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size: 100% 100%;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">JackWang&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-book-reader" style="zoom: 0.6;"></i>
      
      <span>博客</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按标签归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按目录归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按日期分类文章</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>



<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">JackWang&#39;s Blog</div>
        <div class="logo-desc">
            
            JackWang的个人博客
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book-reader"></i>
			
			博客
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
                              <span>按标签归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
                              <span>按目录归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
                              <span>按日期分类文章</    span>

                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('抱歉，这篇文章并不想让所有人都看到，请输入授权密码观看')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/Steps-for-inscribed-cored-grey-relational-analysis-model-IC-GRA-model-calculations.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">数学建模算法6-灰色关联分析与预测</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Python/">
                                <span class="chip bg-color">Python</span>
                            </a>
                        
                            <a href="/tags/Math-Modeling/">
                                <span class="chip bg-color">Math Modeling</span>
                            </a>
                        
                            <a href="/tags/%E7%81%B0%E8%89%B2%E5%85%B3%E8%81%94%E5%88%86%E6%9E%90/">
                                <span class="chip bg-color">灰色关联分析</span>
                            </a>
                        
                            <a href="/tags/%E7%81%B0%E8%89%B2%E9%A2%84%E6%B5%8B/">
                                <span class="chip bg-color">灰色预测</span>
                            </a>
                        
                            <a href="/tags/%E7%81%B0%E8%89%B2%E6%A8%A1%E5%9E%8B/">
                                <span class="chip bg-color">灰色模型</span>
                            </a>
                        
                            <a href="/tags/Grey-relational-analysis/">
                                <span class="chip bg-color">Grey relational analysis</span>
                            </a>
                        
                            <a href="/tags/Grey-Model/">
                                <span class="chip bg-color">Grey Model</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E7%AE%97%E6%B3%95/" class="post-category">
                                数学建模算法
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-01-06
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-06-01
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    8.8k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    34 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <blockquote>
<p>本文主要介绍了数学建模中常见的分析与预测算法：灰色关联分析和灰色预测</p>
</blockquote>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/Steps-for-inscribed-cored-grey-relational-analysis-model-IC-GRA-model-calculations.png" alt="灰色关联分析"></p>
<h1 id="数学建模算法6-灰色关联分析与预测"><a href="#数学建模算法6-灰色关联分析与预测" class="headerlink" title="数学建模算法6-灰色关联分析与预测"></a>数学建模算法6-灰色关联分析与预测</h1><p>灰色关联分析法主要解决对一个系统中多个变量进行分析，从而得出哪些变量和目标变量之间是正相关，哪些是负相关。而灰色预测模型则是在灰色关联分析的基础上，对目标变量未来的发展趋势进行预测的算法。两者都属于灰色系统理论。</p>
<p>因此，灰色系统理论处理的问题为：<strong>相关性分析</strong>以及<strong>预测</strong>两类问题</p>
<p>灰色关联分析是数学建模中非常常用的一系列算法，例如：</p>
<ul>
<li>CUMCM2003A SARS的传播问题</li>
<li>CUMCM2005A 长江水质的评价和预测</li>
<li>CUMCM2006A 出版社的资源配置</li>
<li>CUMCM2006B 艾滋病疗法的评价及疗效的预测问题</li>
<li>CUMCM2007A 中国人口增长预测</li>
</ul>
<p>我们可以更详细的看一下其中的两个问题:</p>
<blockquote>
<p><strong>问题一：SARS传播问题</strong></p>
<p>SARS（Severe Acute Respiratory Syndrome，严重急 性呼吸道综合症, 俗称：非典型肺炎）是21世纪第一 个在世界范围内传播的传染病。SARS的爆发和蔓延 给我国的经济发展和人民生活带来了很大影响，我们 从中得到许多重要的经验和教训，认识到定量地研究 传染病的传播规律、为预测和控制传染病蔓延创造条 件的重要性。请你们对SARS 的传播建立数学模型， 具体要求如下：</p>
<ol>
<li>对附件1所提供的一个早期的模型，评价其合理性和实用性</li>
<li>建立你们自己的模型，说明为什么优于附件1中 的模型；特别要说明怎样才能建立一个真正能够预测 以及能为预防和控制提供可靠、足够的信息的模型， 这样做的困难在哪里？对于卫生部门所采取的措施做 出评论，如：提前或延后5天采取严格的隔离措施， 对疫情传播所造成的影响做出估计。附件2提供的数据供参考。</li>
<li>收集SARS对经济某个方面影响的数据，建立相应的数学模型并进行预测。附件3提供的数据供参考</li>
</ol>
<p><strong>问题二：长江水质的评价和预测</strong></p>
<p>水是人类赖以生存的资源，保护水资源就是保护我们自己，对 于我国大江大河水资源的保护和治理应是重中之重。专家们呼吁 ：“以人为本，建设文明和谐社会，改善人与自然的环境，减少 污染。” 长江是我国第一、世界第三大河流，长江水质的污染程度日趋 严重，已引起了相关政府部门和专家们的高度重视。2004年10月 ，由全国政协与中国发展研究院联合组成“保护长江万里行”考 察团，从长江上游宜宾到下游上海，对沿线21个重点城市做了实 地考察，揭示了一幅长江污染的真实画面，其污染程度让人触目 惊心。为此，专家们提出“若不及时拯救，长江生态10年内将濒 临崩溃”（附件１），并发出了“拿什么拯救癌变长江”的呼唤 （附件2）。</p>
<p>附件3给出了长江沿线17个观测站（地区）近两年多主要水质指标 的检测数据，以及干流上７个观测站近一年多的基本数据（站点距离 、水流量和水流速）。通常认为一个观测站（地区）的水质污染主要 来自于本地区的排污和上游的污水。 一般说来，江河自身对污染物都有一定的自然净化能力，即污染 物在水环境中通过物理降解、化学降解和生物降解等使水中污染物的 浓度降低。反映江河自然净化能力的指标称为降解系数。 事实上，长江干流的自然净化能力可以认为是近似均匀的，根据 检测可知，主要污染物高锰酸盐指数和氨氮的降解系数通常介于 0 . 1 ~ 0 . 5之间，比如可以考虑取0 . 2　(单位：1 /天)。附件4是 “1995~2004年长江流域水质报告”给出的主要统计数据。下面的附 表是国标(GB3838-2002) 给出的《地表水环境质量标准》中4个主要项 目标准限值，其中Ⅰ、Ⅱ、Ⅲ类为可饮用水</p>
<p>请你们研究下列问题：</p>
<ol>
<li>对长江近两年多的水质情况做出定量的综合评价，并分析各地区水质的污染状况。</li>
<li>研究、分析长江干流近一年多主要污染物高锰酸盐指数和氨氮的污染源主要在哪些地区?</li>
<li>假如不采取更有效的治理措施，依照过去10年的主要统计数据， 对长江未来水质污染的发展趋势做出预测分析，比如研究未来10年的 情况</li>
<li>根据你的预测分析，如果未来10年内每年都要求长江干流的Ⅳ类 和Ⅴ类水的比例控制在20%以内，且没有劣Ⅴ类水,那么每年需要处理 多少污水？</li>
<li>你对解决长江水质污染问题有什么切实可行的建议和意见</li>
</ol>
</blockquote>
<p>可以看出来，对于SARS问题，第二、三问都涉及了预测，因此可以用灰色模型求解。而对于长江水质，三、四问同样是需要进行预测的。</p>
<p>此外，数学建模中常用的预测方法用：</p>
<ul>
<li>微分方程模型</li>
<li>灰色预测模型</li>
<li>差分方程模型</li>
<li>马尔可夫预测</li>
<li>时间序列</li>
<li>差值拟合</li>
<li>神经网络</li>
</ul>
<p>不同的预测问题适合不同的预测方法，本文就将讲解其中的灰色关联分析以及灰色预测模型 。</p>
<h2 id="1-灰色系统理论的介绍"><a href="#1-灰色系统理论的介绍" class="headerlink" title="1. 灰色系统理论的介绍"></a>1. 灰色系统理论的介绍</h2><p>灰色系统理论由华中科技大学控制科学与工程系教授，博士生导师邓聚龙于1982年提出的。它是用来解决信息不完备系统的数学方法，它把控制论的观点和方法延伸到复杂的大系统中，将自动控制与运筹学的数学方法相结合，用独树一帜的方法和手段，研究了广泛存在于客观世界中具有灰色性的问题。在短短的时间里，灰色系统理论有了飞速的发展，它的应用已渗透到自然科学和社会经济等许多领域，显示出这门学科的强大生命力，具有广阔的发展前景。</p>
<p><strong>系统分析的经典方法是将系统的行为看做是随机变化的过程，用概率统计方法，从大量历史数据中寻找统计规律，这对于统计数据量较大情况下的处理较为有效，但对于数据量少的贫信息系统的分析则较为棘手。</strong></p>
<p><strong>灰色系统理论研究的是贫信息建模，它提供了贫信息情况下解决系统问题的新途径</strong>。<strong>它把一切随机过程看做是在一定范围内变化的、与时间有关的灰色过程，对灰色量不是从寻找统计规律的角度，通过大样本进行研究，而是用数据生成的方法，将杂乱无章的原始数据整理成规律性较强的生成数列后再作研究</strong>。灰色理论认为系统的行为现象尽管是朦胧的，数据是杂乱无章的，但它毕竟是有序的，有整体功能的，在杂乱无章的数据后面，必然潜藏着某种规律，灰数的生成是从杂乱无章的原始数据中去开拓、发现、寻找这种内在规律。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106152800248.png" alt="维基百科上的介绍"></p>
<p>1989海洋出版社出版英文版《灰色系统论文集》，同年， 英文版国际刊物《灰色系统》杂志正式创刊。目前，国际、 国内200多种期刊发表灰色系统论文，许多国际会议把灰 色系统列为讨论专题。国际著名检索网站已检索我国学者 的灰色系统论著500多次。灰色系统理论应用范围已拓展 到工业、农业、社会、经济、能源、地质、石油等众多科 学领域，成功地解决了生产、生活和科学研究中的大量实 际问题，取得了显著成果。</p>
<p>灰色系统的应用范畴大致分为以下几方面：</p>
<ul>
<li>灰色关联分析</li>
<li>灰色预测（人口预测；灾变预测….等等）</li>
<li>灰色决策</li>
<li>灰色预测控制</li>
</ul>
<h2 id="2-灰色理论的基本概念"><a href="#2-灰色理论的基本概念" class="headerlink" title="2. 灰色理论的基本概念"></a>2. 灰色理论的基本概念</h2><h3 id="1-白色、黑色和灰色系统"><a href="#1-白色、黑色和灰色系统" class="headerlink" title="1. 白色、黑色和灰色系统"></a>1. 白色、黑色和灰色系统</h3><ul>
<li><strong>白色系统</strong>：白色系统是指一个系统的内部特征是完全已知的，即系统的信息是完全充分的。以一个函数为例，我们完全知道这个函数的解析式，因此我们完全知道函数（系统）内部的特征，并且能够给定一个输入，预测其输出。</li>
<li><strong>黑色系统</strong>：黑色系统是指一个系统的内部信息对外界来说是一无所知的，只能通过它与外界的联系来加以观测研究。同样，以函数为例，黑色系统即我们完全不知道解析式的函数，我们完全不知道这个函数（系统）的内部特征，只能通过<strong>大量的数据</strong>，利用插值等方法获得近似的函数解析式（利用统计获得系统的信息），然后给定新的输入，给出的预测不一定对。</li>
<li><strong>灰色系统</strong>：灰色系统内的一部分信息是已知的，另一部分信息是未知的，系统内各因素间有不确定的关系。同样，以函数为例，灰色系统是我们直到部分项的函数，即系统一部分信息是已知的，而另外一部分信息是未知的。</li>
</ul>
<h3 id="2-灰色预测法"><a href="#2-灰色预测法" class="headerlink" title="2. 灰色预测法"></a>2. 灰色预测法</h3><ul>
<li>灰色预测法是一种对含有不确定因素的系统，即灰色系统进行预测的方法</li>
<li>灰色预测是对既含有已知信息又含有不确定信息的系统进行预则，就是对在一定范围内变化的、与时间有关的灰色过程进行预测的过程</li>
</ul>
<p>大体上来说，灰色预测通过鉴别系统因素之间发展趋势的相异程度，即进行关联分析，并可对原始数据进行生成处 理来寻找系统变动的规律，生成有较强规律性的数 据序列,然后建立相应的微分方程模型，从而预测 事物未来发展趋势的状况</p>
<p>即灰色预测首先判断目标量和哪些量之间是有关系的。例如预测人口增长，那么受限通过灰色关联分析分析得到人口增长是和经济增长有关系、交通发达等等因素有关系。</p>
<p>接下来对原始数据进行生成处理，例如累加或者累减操作生成一个原始数据的序列。而后对该序列建立一个微分方程的模型，最后进行预测</p>
<p>注意，<strong>灰色预测法要求对预测对象的值的观测是等时距的</strong>。从而利用等时距观测到的反映预测对象特征的一系列数量值构造灰色预测模型，预测未来某一时刻的预测对象的特征量，或预测对象达到某一特征量的时间。</p>
<h3 id="3-灰色预测的四种常见类型"><a href="#3-灰色预测的四种常见类型" class="headerlink" title="3. 灰色预测的四种常见类型"></a>3. 灰色预测的四种常见类型</h3><h4 id="A-灰色时间序列预测"><a href="#A-灰色时间序列预测" class="headerlink" title="A. 灰色时间序列预测"></a>A. 灰色时间序列预测</h4><p>用观察到的反映预测对象特征的时间序 列来构造灰色预测模型，预测未来某一时刻 的特征量，或达到某一特征量的时间。比如股市分析</p>
<h4 id="B-畸变预测"><a href="#B-畸变预测" class="headerlink" title="B. 畸变预测"></a>B. 畸变预测</h4><p>通过灰色模型预测异常值出现的时刻， 预测异常值什么时候出现在特定时区内。例如地震预测、异常气候预测、设备异常分析</p>
<h4 id="C-系统预测"><a href="#C-系统预测" class="headerlink" title="C. 系统预测"></a>C. 系统预测</h4><p>通过对系统行为特征指标建立一组相互 关联的灰色预测模型，预测系统中众多 变量间的相互协调关系的变化。</p>
<h4 id="D-拓扑预测"><a href="#D-拓扑预测" class="headerlink" title="D. 拓扑预测"></a>D. 拓扑预测</h4><p>将原始数据做曲线，在曲线上按定值寻 找该定值发生的所有时点，并以该定值 为框架构成时点数列，然后建立模型预 测该定值所发生的时点。例如对寻找系统中某个变量等于定值时候的时间点。例如温度控制，温度为27度的时候要升温到30度。那么找到所有温度为27度的时间即拓扑预测。</p>
<h3 id="4-灰色关联度"><a href="#4-灰色关联度" class="headerlink" title="4. 灰色关联度"></a>4. 灰色关联度</h3><p>大千世界里的客观事物往往现象复杂，因素繁多。我们经常要对系统进行因素分析，这些<strong>因素中哪些对系统来讲是主要的，哪些是次要的，哪些需要发展，哪些需要抑制，哪些是潜在的，哪些是明显的？一般来讲，这些都是我们极为关心的问题</strong>。事实上，<strong>因素间关联性如何、关联程度如何量化等问题是系统分析的关键</strong>。</p>
<ul>
<li><strong>例如人口问题</strong>。人和社会在一起构成一个系统，影响人口发展变化的因素有社会方面的诸如计划生育、社会治安、社会生活方式等；有经济方面的诸如国民收入、社会福利、社会保险等；还有医疗方面的诸如医疗条件、医疗水平等.……也就是说，人口是多种因素互相关联、互相制约的系统，对这些因素进行分析将有有助于人们对人口的未来预测及人口控制工作。</li>
</ul>
<p>灰色关联度就是衡量因素间的关联性，对关联性进行量化的指标。</p>
<p><strong>因素分析的基本方法过去主要是采用回归分析（即在大量的数据的基础上，通过回归等手段获得因变量和自变量的解析式的这类方法）等办法</strong>，但回归分析的办法有很多欠缺，如要求大量数据、计算量大以及可能出现反常情况等。为克服以上弊病，就产生了灰色关联度分析</p>
<p><strong>灰色关联度是分析向量与向量之间以及矩阵与矩阵之间的关联度</strong>。既然计算关联度，一定是计算某一个待比较的数列与参照物（参考数列）之间的相关程度。</p>
<h2 id="3-灰色关系分析"><a href="#3-灰色关系分析" class="headerlink" title="3. 灰色关系分析"></a>3. 灰色关系分析</h2><h3 id="A-灰色关联度的计算"><a href="#A-灰色关联度的计算" class="headerlink" title="A. 灰色关联度的计算"></a>A. 灰色关联度的计算</h3><p>灰色关联其实计算的是对比数列和参考数列之间的相似程序/离散程度</p>
<ol>
<li><p>选取参考数列</p>
<script type="math/tex; mode=display">
X_0=\{X_0(k)|k=1,2,\cdots,n\}=(X_0(1), X_0(2),\cdots, X_0(n))</script><p>上式中，$k$表示时刻</p>
</li>
<li><p>假设存在$m$个比较数列</p>
<script type="math/tex; mode=display">
X_i=\{X_i(k)|k=1,\cdots,n\}=(X_i(1), X_i(2),\cdots, x_i(n)),\qquad i=1,2,\cdots,m</script><p>则称</p>
<script type="math/tex; mode=display">
\zeta_i(k)=\frac{\min_{i}\min_k|X_0(k)-x_i(k)|+\rho\max_i\max_k|X_0(k)-X_i(k)|} {|X_0(k)-X_i(k)|+\rho \max_i\max_k|X_0(k)-X_i(k)|}</script><p>为比较数列$X_i$对参考数列$X_0$在$k$时刻的<strong>关联系数</strong>。其中$\rho\in[0,\infin)$为<strong>分辨系数</strong>，一般而言，$\rho\in[0,1]$。$\rho$越大，分辨率越大；$\rho$越小，分辨率越小，$\rho$一般取0.5。直观的理解即$m$个比较数列全局的最小差距和最大差距的和与当前数列的第$k$项和全局的最大和。描述了当前项的分散度。</p>
</li>
<li><p>关联系数描述了比较数列和参考数列在某一时刻上的关联度，而对于数列整体而言，定义</p>
<script type="math/tex; mode=display">
r_i=\frac 1 n\sum_{k=1}^n\zeta_i(k)</script><p>为比较数列$X_i$相对于参考数列$X_0$的关联度。</p>
</li>
<li><p>此外，由于计算关联系数的时候取了绝对值，因此无法通过关联度判断是正相关还是负相关，为此，引入下面的式子进行判断</p>
<script type="math/tex; mode=display">
\begin {aligned}
\sigma_i &= \sum_{k=1}^nkX_i(k)-\sum_{k=1}^nX_i(k)\sum_{k=1}^n\frac{k}{n}\\
\sigma_n &= \sum_{k=1}^nk^2-(\sum_{k=1}^nk)2/n
\end {aligned}</script><p>则有</p>
<script type="math/tex; mode=display">
\begin{cases}
sign(\frac{\sigma_i}{\sigma_n})=sign(\frac{\sigma_j}{\sigma_n}), & 则X_i和X_j正相关\\
sign(\frac{\sigma_i}{\sigma_n})=-sign(\frac{\sigma_j}{\sigma_n}), & 则X_i和X_j负相关\\
\end{cases}</script></li>
</ol>
<h3 id="B-灰色综合分析的案例"><a href="#B-灰色综合分析的案例" class="headerlink" title="B. 灰色综合分析的案例"></a>B. 灰色综合分析的案例</h3><blockquote>
<p>利用灰色关联分析对6位教师工作状况进行综合分析（即判断那个老师能力强，那个老师比较好）：</p>
<ol>
<li><p>分析指标包括：专业素质、外语水平、教学工作量、科研成果、 论文、著作与出勤</p>
</li>
<li><p>对原始数据经处理后得到以下数值</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106185111044.png" alt="指标表"></p>
</li>
</ol>
</blockquote>
<p>注意：</p>
<ol>
<li>在计算灰色关联度的时候，各个指标注意值域要相同 </li>
<li>这里由于是通过多个不同的指标来衡量老师，因此当然可以用层次分析法</li>
</ol>
<p>求解：</p>
<ol>
<li><p>计算$|x_0(k)-x_j(k)|$，如下表1</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106200138468.png" alt="表1"></p>
</li>
<li><p><strong>求最值</strong>：$\min=0，\max=7$</p>
</li>
<li><p>计算相关系数，如下表2</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106200238977.png" alt="表2"></p>
</li>
<li><p><strong>分别计算每个人各指标关联系数的均值（关联序）</strong>：如下表3</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106200315535.png" alt="表3"></p>
</li>
</ol>
<h3 id="C-灰色关联度计算代码"><a href="#C-灰色关联度计算代码" class="headerlink" title="C. 灰色关联度计算代码"></a>C. 灰色关联度计算代码</h3><p>Python实现的代码如下：</p>
<pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">from</span> typing <span class="token keyword">import</span> <span class="token operator">*</span>
<span class="token keyword">from</span> pathlib <span class="token keyword">import</span> Path

<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">from</span> numpy<span class="token punctuation">.</span>lib<span class="token punctuation">.</span>function_base <span class="token keyword">import</span> iterable
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd


<span class="token keyword">class</span> <span class="token class-name">GrayRationalAnalysis</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> df<span class="token punctuation">:</span> pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">,</span> reference_vector<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>int<span class="token punctuation">,</span> np<span class="token punctuation">.</span>ndarray<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> None<span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>df <span class="token operator">=</span> df
        self<span class="token punctuation">.</span>reference_vector <span class="token operator">=</span> reference_vector
        self<span class="token punctuation">.</span>indices <span class="token operator">=</span> df<span class="token punctuation">.</span>columns

    <span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> rho<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        refer_table <span class="token operator">=</span> self<span class="token punctuation">.</span>df<span class="token punctuation">.</span>subtract<span class="token punctuation">(</span>self<span class="token punctuation">.</span>reference_vector<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>abs<span class="token punctuation">(</span><span class="token punctuation">)</span>
        min_value<span class="token punctuation">,</span> max_value <span class="token operator">=</span> refer_table<span class="token punctuation">.</span>min<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>min<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> refer_table<span class="token punctuation">.</span>max<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>max<span class="token punctuation">(</span><span class="token punctuation">)</span>

        correlation_table<span class="token punctuation">:</span> pd<span class="token punctuation">.</span>DataFrame <span class="token operator">=</span> <span class="token punctuation">(</span>min_value <span class="token operator">+</span> rho <span class="token operator">*</span> max_value<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span>refer_table <span class="token operator">+</span> rho <span class="token operator">*</span> max_value<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>correlation_table<span class="token punctuation">.</span>sum<span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">/</span>self<span class="token punctuation">.</span>df<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>


    @staticmethod
    <span class="token keyword">def</span> <span class="token function">build_from_csv</span><span class="token punctuation">(</span>csv_path<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>str<span class="token punctuation">,</span> Path<span class="token punctuation">]</span><span class="token punctuation">,</span> reference_vector<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>int<span class="token punctuation">,</span> np<span class="token punctuation">.</span>ndarray<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">9</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token string">'GrayRationalAnalysis'</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">"""
        Notes:
            build_from_csv用于从CSV文件中读取数据并构建灰色分析对象
        """</span>
        csv_path <span class="token operator">=</span> Path<span class="token punctuation">(</span>csv_path<span class="token punctuation">)</span> <span class="token keyword">if</span> isinstance<span class="token punctuation">(</span>csv_path<span class="token punctuation">,</span> str<span class="token punctuation">)</span> <span class="token keyword">else</span> csv_path
        df <span class="token operator">=</span> pd<span class="token punctuation">.</span>read_csv<span class="token punctuation">(</span>csv_path<span class="token punctuation">,</span> header<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> index_col<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> isinstance<span class="token punctuation">(</span>reference_vector<span class="token punctuation">,</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
            reference_vector<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> np<span class="token punctuation">.</span>full<span class="token punctuation">(</span>df<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> fill_value<span class="token operator">=</span>reference_vector<span class="token punctuation">)</span>
        <span class="token keyword">elif</span> iterable<span class="token punctuation">(</span>reference_vector<span class="token punctuation">)</span><span class="token punctuation">:</span>
            reference_vector <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>reference_vector<span class="token punctuation">)</span>
            <span class="token keyword">assert</span> df<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> len<span class="token punctuation">(</span>reference_vector<span class="token punctuation">)</span><span class="token punctuation">,</span> f<span class="token string">"维度不匹配"</span>

        <span class="token keyword">return</span> GrayRationalAnalysis<span class="token punctuation">(</span>df<span class="token operator">=</span>df<span class="token punctuation">,</span> reference_vector<span class="token operator">=</span>reference_vector<span class="token punctuation">)</span>



<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    gra <span class="token operator">=</span> GrayRationalAnalysis<span class="token punctuation">.</span>build_from_csv<span class="token punctuation">(</span>Path<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>parent<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"./test.csv"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> reference_vector<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="4-灰色生成数列"><a href="#4-灰色生成数列" class="headerlink" title="4. 灰色生成数列"></a>4. 灰色生成数列</h2><p>灰色系统理论认为，对于一个系统而言，尽管客观表象复杂，但总是有整体功能的，因此必然蕴含某种内在规律。关键在于如何选择适当的方式去挖掘和利用它。<strong>灰色系统是通过对原始数据的整理来寻求其变化规律的</strong>， 这是一种就数据寻求数据的现实规律的途径，即为灰色序列的生成。<strong>一切灰色序列都能通过某种生成弱化其随机性，显现其规律性</strong>。数据生成的常用方式：</p>
<ul>
<li>累加生成</li>
<li>累减生成</li>
<li>加权累加生成</li>
</ul>
<p>生成数列的目的在于原始的描述观测的特征量的值的数列具有一定的随机性，而通过多种不同的方式对原始数据进行变换（生成新的数列）的方法就可以减弱其随机性，使得原始数列的特征更加易于发现。</p>
<h3 id="A-累加生成数列"><a href="#A-累加生成数列" class="headerlink" title="A. 累加生成数列"></a>A. 累加生成数列</h3><p><strong>把数列各项（时刻）数据依次累加的过程称为累加生成过程（AGO）</strong>。由累加生成过程所得的数列称为<strong>累加生成数列</strong>。</p>
<p>设原始数列为</p>
<script type="math/tex; mode=display">
x^{(0)}=(x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n)</script><p>令</p>
<script type="math/tex; mode=display">
x^{(1)}(k)=\sum_{i=1}^kx^{(0)}(i), \qquad k=1,2,\cdots,n</script><p>则称数列</p>
<script type="math/tex; mode=display">
x^{(1)}=(x^{(1)}(1), x^{(1)}(2), \cdots, x^{(1)}(n)</script><p>为数列$x^{(0)}$的<strong>一次累加生成数列</strong>。类似的，称</p>
<script type="math/tex; mode=display">
x^{(r)}=(x^{(r)}(1), x^{(r)}(2), \cdots, x^{(r)}(n),其中 x^{(r)}(k)=\sum_{i=1}^kx^{(r-1)}(i), \quad k=1,2,\cdots,n</script><p>为$x^{(0)}$的<strong>r次累加生成数列</strong></p>
<p>一般经济数列都是非负数列。累加生成能使任 意非负数列、摆动的与非摆动的，转化为非减 的、递增的。例如下图：</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106204501736.png" alt="累加生成数列" style="zoom: 67%;"></p>
<p>那么原始数列每一项所含的随机影响相对于全体来说就无法造成大的影响，此时找出累加生成数列的规律，而后反推回去得到原始数列的预测就有可能，例如下面的例子。</p>
<p>对于每一年的作物产量进行预测，则由于每年都有病虫害问题导致年产量的曲线难以拟合，而进行一次累加后得到累计年产量曲线则表明在总体上作物产量是以指数关系增长的。对于钢产量也是同理。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106204702753.png" alt="累加生成数列容易观察得到总体服从的规律" style="zoom:67%;"></p>
<h3 id="B-累减生成数列"><a href="#B-累减生成数列" class="headerlink" title="B. 累减生成数列"></a>B. 累减生成数列</h3><p><strong>对于原始数据列依次做前后相邻的两个数据相减的运算过程称为累减生成过程（IAGO）</strong></p>
<p>设原始数列为</p>
<script type="math/tex; mode=display">
x^{(1)}=(x^{(1)}(1), x^{(1)}(2), \cdots, x^{(1)}(n)</script><p>令</p>
<script type="math/tex; mode=display">
x^{(0)}(k)=x^{(1)}(k)-x^{(1)}(k-1), \qquad k=2,3,\cdots,n</script><p>则称数列</p>
<script type="math/tex; mode=display">
x^{(0)}=(x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n)</script><p>为数列$x^{(1)}$的<strong>一次减生成数列</strong>。</p>
<p>其实从符号上可以看出，累加是累减的逆操作。</p>
<p>对于累减生成数列而言，其具有求导的性质，例如下面的例子</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106205143207.png" alt="累减数列具有求导的性质" style="zoom:50%;"></p>
<h3 id="C-加权邻值生成数列"><a href="#C-加权邻值生成数列" class="headerlink" title="C. 加权邻值生成数列"></a>C. 加权邻值生成数列</h3><p>设原始数列为</p>
<script type="math/tex; mode=display">
x^{(0)}=(x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n)</script><p>称$(x^{(0)}(k-1),x^{(0)}(k-1))$为一对邻值，其中称$x^{(0)}(k-1)$为前邻值，称$x^{(0)}(k-1)$为后邻值。</p>
<p>对于任意常数$\alpha\in [0,1]$，取</p>
<script type="math/tex; mode=display">
z^{(0)}(k)=\alpha x^{(0)}(k)+(1-\alpha)x^{(0)}(k-1),\qquad k=2,3,\cdots,n</script><p>则由此得到的数列</p>
<script type="math/tex; mode=display">
z^{(0)}=(z^{(0)}(1), z^{(0)}(2), \cdots, z^{(0)}(n)</script><p>称为数列$x^{(0)}$在权$\alpha$下的<strong>邻值生成数列</strong>，权$\alpha$称为生成系数</p>
<p>若$\alpha=0.5$，则称数列为均值生成数列，也称为等权邻值生成数列</p>
<h2 id="5-灰色模型"><a href="#5-灰色模型" class="headerlink" title="5. 灰色模型"></a>5. 灰色模型</h2><h3 id="A-GM-1-1"><a href="#A-GM-1-1" class="headerlink" title="A. $GM(1,1)$"></a>A. $GM(1,1)$</h3><blockquote>
<p>灰色系统理论是基于关联空间、光滑离散函数等概念定义灰导数与灰微分方程，进而用离散数据列建立微分方程形式的动态模型，即灰色模型是利用离散随机数经过生成变为随机性被显著削弱而且较有规律的生成数，建立起的微分方程形式的模型，这样便于对其变化过程进行研究和描述。</p>
</blockquote>
<p>上面的这段话的关键点：</p>
<ul>
<li>灰色系统理论中定义了定义灰导数和灰微分方程，而灰色模型是基于这些基本概念定义的</li>
<li>灰色模型是一个<strong>微分方程模型</strong>，该微分方程模型是在随机性被削弱的生成数的基础上构建的</li>
</ul>
<p>设原始数列为</p>
<script type="math/tex; mode=display">
x^{(0)}=(x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n)</script><p>且其一次累加数列为</p>
<script type="math/tex; mode=display">
x^{(1)}=(x^{(1)}(1), x^{(1)}(2), \cdots, x^{(1)}(n)</script><p>则，定义数列$x^{(1)}$的<strong>灰导数</strong>为</p>
<script type="math/tex; mode=display">
d(k)=x^{(0)}(k)=x^{(1)}(k)-x^{(1)}(k-1)</script><p>设$z^{(1)}(k)$为数列$x^{(1)}$的邻值生成数列，即</p>
<script type="math/tex; mode=display">
z^{(1)}(k)=\alpha x^{(1)}(k)+(1-\alpha)x^{(1)}(k-1)</script><p>则定义$GM(1,1)$的<strong>灰微分方程</strong>为</p>
<script type="math/tex; mode=display">
d(k)+az^{(1)}(k)=b\\或\\x^{(0)}(k)+az^{(1)}(k)=b</script><p>上式中，$x^{(0)}(k)$是灰导数。而称$a$为<strong>发展系数</strong>，$z^{(1)}(k)$为<strong>白化背景值</strong>，b为<strong>灰作用量</strong>。</p>
<p>将时刻$k=2,3,\cdots,n$带入灰微分方程得</p>
<script type="math/tex; mode=display">
\begin{cases}
x^{(0)}(2)+az^{(1)}(2)=b\\
x^{(0)}(3)+az^{(1)}(3)=b\\
\cdots\\
x^{(0)}(n)+az^{(1)}(n)=b\\
\end{cases}</script><p>将上式向量化，则设</p>
<script type="math/tex; mode=display">
\vec u = 
\begin{bmatrix}
a\\b
\end{bmatrix}</script><script type="math/tex; mode=display">
\vec Y = 
\begin{bmatrix}
x^{(0)}(2)\\
x^{(0)}(3)\\
\vdots\\
x^{(0)}(n)
\end{bmatrix}</script><script type="math/tex; mode=display">
B = 
\begin{bmatrix}
-z^{(1)}(2) & 1\\
-z^{(1)}(3) & 1\\
\vdots\\
-z^{(1)}(n) & 1
\end{bmatrix}</script><p>则$GM(1,1)$可以表示为</p>
<script type="math/tex; mode=display">
\vec Y=B\vec u</script><p>由于$\vec Y$和$B$都可以根据输入获得，因此问题的关键在于求解$\vec u$</p>
<p>微分方程是线性微分方程，因此可以采用一元线性回归，即最小二乘法求估计值</p>
<script type="math/tex; mode=display">
\vec u = \begin{bmatrix}a\\b \end{bmatrix}=(B^TB)^{-1}B^TY</script><h3 id="B-白化型-GM-1-1"><a href="#B-白化型-GM-1-1" class="headerlink" title="B. 白化型$GM(1,1)$"></a>B. 白化型$GM(1,1)$</h3><p>$GM(1,1)$是离散的方程，因为其中的导数和微分都是灰导数和灰积分。然而在有的时候，我们如果数列中的每一项之间都是沿着时间收集的，即数列中的每一项都是和时间有关的。那么我们就希望能够用含有时间$t$的微分方程来描述。因此，含有时间$t$的灰色方程即白化型$GM(1,1)$</p>
<p>对于$GM(1,1)$中的灰微分方程</p>
<script type="math/tex; mode=display">
x^{(0)}(k)+az^{(1)}(k)=b</script><p>若将灰导数$x^{(0)}(k)$的每一项$k$（$k=2,3,\cdots,n$）视为连续的变量$t$，即将项的次序视为时间。则$x^{(1)}$是时间$t$的函数，即$x^{(1)}=x^{(1)}(t)$。则$x^{(0)}(k)$就对应导数，即</p>
<script type="math/tex; mode=display">
x^{(0)}(k)=x^{(0)}(t)=\frac{dx^{(1)}(t)}{dt}</script><p>而白化背景就相当于$\alpha x^{(1)}(t)$，故若将$GM(1,1)$视为连续的项，则有</p>
<script type="math/tex; mode=display">
\frac{dx^{(1)}(t)}{dt}+\alpha x^{(1)}(t)=b</script><p>上式两边对$t$同时积分，有：</p>
<script type="math/tex; mode=display">
\int_{k-1}^k(\frac{dx^{(1)}(t)}{dt})dt+\int_{k-1}^k ax^{(1)}(t)dt = \int_{k-1}^k bdt</script><p>左边第一项为$\frac{dx^{(1)}(t)}{dt}$的原函数的第$k$项减第$k-1$的值，即</p>
<script type="math/tex; mode=display">
x^{(1)}{(k)}-x^{(1)}(k-1)=x^{(0)}(k)</script><p>而第二项要求$x^{(1)}(t)$的原函数，则</p>
<script type="math/tex; mode=display">
\int_{k-1}^k ax^{(1)}dt\approx az^{(1)}</script><h2 id="6-GM-1-1-的建模步骤"><a href="#6-GM-1-1-的建模步骤" class="headerlink" title="6. $GM(1,1)$的建模步骤"></a>6. $GM(1,1)$的建模步骤</h2><p>设原始数列为</p>
<script type="math/tex; mode=display">
x^{(0)}=(x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n)</script><ol>
<li><p><strong>数据的检验与处理</strong>： 为了保证$GM(1,1)$建模方法的可行性，需要对原始数列做必要的检验处理。</p>
<ol>
<li><p><strong>计算原始数列的级比</strong>：</p>
<script type="math/tex; mode=display">
\lambda(k)=\frac{x^{(0)}(k-1)}{x^{(0)}(k)},\qquad k=2,2,\cdots,n</script></li>
<li><p>若所有级比都落在区间</p>
<script type="math/tex; mode=display">
X=(e^{-\frac{2}{n+1}}, e^{\frac{2}{n+1}})</script><p>内，则可以直接对原始数列$x^{(0)}$建立$GM(1,1)$模型并进行灰色预测，否则由于不满足灰色模型的理论，因此不能直接用灰色灰色模型</p>
</li>
<li><p>否则需要对原始数列做适当的变换处理，例如平移（加常数）</p>
</li>
</ol>
</li>
<li><p><strong>建立$GM(1,1)$模型</strong>：</p>
<ol>
<li><p>假设通过第一步，得到了满足要求的数列$x^{(0)}$</p>
</li>
<li><p>则建立白化模型</p>
<script type="math/tex; mode=display">
x^{(0)}(k)+az^{(1)}(k)=b</script><p>通过线性回归求得$a$和$b$的值，从而得到模型</p>
<script type="math/tex; mode=display">
x^{(1)}(t)=(x^{(0)}(1)-\frac b a)e^{-a(t-1)}+\frac b a</script></li>
<li><p>从而得到预测值</p>
<script type="math/tex; mode=display">
x^{(1)}(k+1)=(x^{(0)}(1)-\frac b a)e^{-ak}+\frac b a,\qquad k=1,2,\cdots,n-1</script></li>
<li><p>最终得到预测值</p>
<script type="math/tex; mode=display">
x^{(0)}(k+1)=x^{(1)}(k+1)-x^{(1)}(k),\qquad k=1,2,\cdots,n-1</script></li>
</ol>
</li>
<li><p><strong>检验预测值</strong>：为了检测$GM(1,1)$的预测是否正确，还需要对预测进行检测</p>
<ol>
<li><p><strong>残差检验</strong>：计算相对残差，即原值减去预测值比上原值</p>
<script type="math/tex; mode=display">
\varepsilon(k)=\frac{x^{(0)}(k)-\hat {x}^{(0)}(k)}{x^{(0)}(k)},\qquad k=1,2,\cdots,n</script><p>若对于所有的$|\varepsilon(k)|&lt;0.1$，则可以认为模型的预测达到较高的精度；否则，若对于所有的$|\varepsilon(k)|&lt;0.2$，则认为模型的预测达到一般精度</p>
</li>
<li><p><strong>级比检验</strong>：计算</p>
<script type="math/tex; mode=display">
\rho(k)=1-\frac{1-0.5a}{1+0.5a}\lambda(k)</script><p>若对于所有的$|\rho(k)|&lt;0.1$，则认为模型的预测达到了较高的精度；否则，若对于所有的$|\rho(k)|&lt;0.2$，则认为模型达到了一般的要求</p>
</li>
</ol>
</li>
</ol>
<h2 id="7-灰色预测Python求解"><a href="#7-灰色预测Python求解" class="headerlink" title="7. 灰色预测Python求解"></a>7. 灰色预测Python求解</h2><p>基于Numpy和Pandas实现的灰色预测模型代码如下</p>
<h3 id="A-代码"><a href="#A-代码" class="headerlink" title="A. 代码"></a>A. 代码</h3><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">from</span> pathlib <span class="token keyword">import</span> Path
<span class="token keyword">import</span> re
<span class="token keyword">from</span> typing <span class="token keyword">import</span> Union

<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> scipy<span class="token punctuation">.</span>stats <span class="token keyword">as</span> scist



<span class="token keyword">class</span> <span class="token class-name">GrayForcast</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> series<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">,</span> pd<span class="token punctuation">.</span>Series<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> None<span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>index<span class="token punctuation">:</span> pd<span class="token punctuation">.</span>Index <span class="token operator">=</span> series<span class="token punctuation">.</span>index
        self<span class="token punctuation">.</span>x0 <span class="token operator">=</span> series<span class="token punctuation">.</span>to_numpy<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">train</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> verbose<span class="token punctuation">:</span> bool <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true"># 计算级比</span>
        right_moved<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>x0<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>lambdas<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> right_moved <span class="token operator">/</span> self<span class="token punctuation">.</span>x0

        <span class="token comment" spellcheck="true"># 级比检验</span>
        n <span class="token operator">=</span> len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>x0<span class="token punctuation">)</span>
        <span class="token keyword">assert</span> <span class="token punctuation">(</span>l<span class="token punctuation">:</span><span class="token operator">=</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>exp<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token operator">/</span>n<span class="token punctuation">)</span> <span class="token operator">&lt;</span> self<span class="token punctuation">.</span>lambdas<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token punctuation">(</span>self<span class="token punctuation">.</span>lambdas<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> np<span class="token punctuation">.</span>exp<span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">/</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>all<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> f<span class="token string">"级比检验未通过:{l}, 其中第{list(np.where(l==False)[0])}位:{self.x0[1:][~l]} 出错，数据不适合直接进行灰色预测"</span>
        <span class="token keyword">if</span> verbose<span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"级比检验通过"</span><span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># GM(1,1)建模</span>
        <span class="token comment" spellcheck="true"># 一次累加</span>
        self<span class="token punctuation">.</span>x1 <span class="token operator">=</span> np<span class="token punctuation">.</span>cumsum<span class="token punctuation">(</span>self<span class="token punctuation">.</span>x0<span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># 构建B和Y矩阵</span>
        Y <span class="token operator">=</span> self<span class="token punctuation">.</span>x0<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
        right_moved <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">*</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>x1<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        left_moved <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">*</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>x1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        B <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span>tup<span class="token operator">=</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span> <span class="token operator">*</span> <span class="token punctuation">(</span>left_moved <span class="token operator">+</span> right_moved<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">,</span> np<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span>right_moved<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>T

        <span class="token comment" spellcheck="true"># 线性回归</span>
        self<span class="token punctuation">.</span>u <span class="token operator">=</span> np<span class="token punctuation">.</span>linalg<span class="token punctuation">.</span>inv<span class="token punctuation">(</span>B<span class="token punctuation">.</span>T @ B<span class="token punctuation">)</span> @ B<span class="token punctuation">.</span>T @ Y

        <span class="token comment" spellcheck="true"># 模型检验</span>
        x0_pred <span class="token operator">=</span> self<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>items<span class="token operator">=</span><span class="token number">7</span><span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># 残差检验</span>
        relative_residual <span class="token operator">=</span> np<span class="token punctuation">.</span>abs<span class="token punctuation">(</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>x0 <span class="token operator">-</span> x0_pred<span class="token punctuation">)</span> <span class="token operator">/</span> self<span class="token punctuation">.</span>x0<span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>l<span class="token punctuation">:</span><span class="token operator">=</span> <span class="token punctuation">(</span>relative_residual <span class="token operator">&lt;</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>all<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"残差检验通过，所有残差小于0.1，达到高精度"</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>l<span class="token punctuation">:</span><span class="token operator">=</span> <span class="token punctuation">(</span>relative_residual <span class="token operator">&lt;</span> <span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>all<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"残差检验通过，所有残差小于0.2，达到一般精度要求"</span><span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                <span class="token keyword">assert</span> <span class="token boolean">False</span><span class="token punctuation">,</span> f<span class="token string">"残差检验未通过，第{np.where(l==False)[0]}位:{relative_residual[~l]}大于0.2"</span>

        <span class="token comment" spellcheck="true"># 级比检验</span>
        rho <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">-</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">-</span> self<span class="token punctuation">.</span>u<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">+</span> self<span class="token punctuation">.</span>u<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token operator">*</span> self<span class="token punctuation">.</span>lambdas
        <span class="token keyword">if</span> <span class="token punctuation">(</span>l<span class="token punctuation">:</span><span class="token operator">=</span> <span class="token punctuation">(</span>rho<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>all<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"级比检验通过，所有级比小于0.1，达到高精度"</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>l<span class="token punctuation">:</span><span class="token operator">=</span> <span class="token punctuation">(</span>rho<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> <span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>all<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"级比检验通过，所有级比小于0.2，达到一般精度要求"</span><span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                <span class="token keyword">assert</span> <span class="token boolean">False</span><span class="token punctuation">,</span> f<span class="token string">"级比检验未通过，第{np.where(l==False)[0]}位:{rho[1:][~l]}大于0.2"</span>
        rho<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> np<span class="token punctuation">.</span>nan

        <span class="token keyword">if</span> verbose<span class="token punctuation">:</span>
            df <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span>tup<span class="token operator">=</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>x0<span class="token punctuation">,</span> x0_pred<span class="token punctuation">,</span> self<span class="token punctuation">.</span>x0<span class="token operator">-</span>x0_pred<span class="token punctuation">,</span> relative_residual<span class="token operator">*</span><span class="token number">100</span><span class="token punctuation">,</span> rho<span class="token punctuation">)</span><span class="token punctuation">)</span>
            df <span class="token operator">=</span> pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>df<span class="token punctuation">.</span>T<span class="token punctuation">,</span> index<span class="token operator">=</span>self<span class="token punctuation">.</span>index<span class="token punctuation">,</span> columns<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">"原始值"</span><span class="token punctuation">,</span><span class="token string">"预测值"</span><span class="token punctuation">,</span><span class="token string">"残差"</span><span class="token punctuation">,</span><span class="token string">"相对残差(%)"</span><span class="token punctuation">,</span><span class="token string">"级比偏差"</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>df<span class="token punctuation">)</span>



    <span class="token keyword">def</span> <span class="token function">predict</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> items<span class="token punctuation">:</span> int<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> np<span class="token punctuation">.</span>ndarray<span class="token punctuation">:</span>
        a<span class="token punctuation">,</span> b <span class="token operator">=</span> self<span class="token punctuation">.</span>u<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>u<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span>
        <span class="token keyword">def</span> <span class="token function">x1_kp1</span><span class="token punctuation">(</span>k<span class="token punctuation">:</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> k <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
                <span class="token keyword">return</span> self<span class="token punctuation">.</span>x0<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                <span class="token keyword">return</span> <span class="token punctuation">(</span>self<span class="token punctuation">.</span>x0<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">-</span> b<span class="token operator">/</span>a<span class="token punctuation">)</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>exp<span class="token punctuation">(</span><span class="token operator">-</span>a <span class="token operator">*</span> k<span class="token punctuation">)</span> <span class="token operator">+</span> b<span class="token operator">/</span>a
        x1_pred <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>x1_kp1<span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span>items<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        right_moved <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">(</span>x1_pred<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        x0_pred <span class="token operator">=</span> x1_pred <span class="token operator">-</span> right_moved
        <span class="token keyword">return</span> x0_pred


    @staticmethod
    <span class="token keyword">def</span> <span class="token function">preprocess_donothing</span><span class="token punctuation">(</span>gf<span class="token punctuation">:</span> <span class="token string">"GrayForcast"</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token string">"GrayForcast"</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> gf

    @staticmethod
    <span class="token keyword">def</span> <span class="token function">build_from_csv</span><span class="token punctuation">(</span>csv_path<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>str<span class="token punctuation">,</span> Path<span class="token punctuation">]</span><span class="token punctuation">,</span> target_col<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>None<span class="token punctuation">,</span> int<span class="token punctuation">,</span> str<span class="token punctuation">]</span><span class="token operator">=</span>None<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token string">"GrayForcast"</span><span class="token punctuation">:</span>
        csv_path <span class="token operator">=</span> csv_path <span class="token keyword">if</span> isinstance<span class="token punctuation">(</span>csv_path<span class="token punctuation">,</span> Path<span class="token punctuation">)</span> <span class="token keyword">else</span> Path<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>parent<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>csv_path<span class="token punctuation">)</span>
        df <span class="token punctuation">:</span> pd<span class="token punctuation">.</span>DataFrame <span class="token operator">=</span> pd<span class="token punctuation">.</span>read_csv<span class="token punctuation">(</span>csv_path<span class="token punctuation">,</span> header<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> index_col<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>

        <span class="token keyword">assert</span> <span class="token punctuation">(</span>multiple<span class="token punctuation">:</span><span class="token operator">=</span>df<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">and</span> target_col <span class="token keyword">is</span> <span class="token operator">not</span> None<span class="token punctuation">,</span> f<span class="token string">"CSV文件存在多个序列：{df.columns}，请指定target_col参数指定需要预测的序列"</span>

        <span class="token keyword">if</span> multiple<span class="token punctuation">:</span>
            <span class="token keyword">if</span> isinstance<span class="token punctuation">(</span>target_col<span class="token punctuation">,</span> str<span class="token punctuation">)</span><span class="token punctuation">:</span>
                target_col <span class="token operator">=</span> df<span class="token punctuation">.</span>T<span class="token punctuation">.</span>loc<span class="token punctuation">[</span>target_col<span class="token punctuation">]</span>
            <span class="token keyword">elif</span> isinstance<span class="token punctuation">(</span>target_col<span class="token punctuation">,</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
                target_col <span class="token operator">=</span> df<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> target_col<span class="token punctuation">]</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            target_col <span class="token operator">=</span> df
        <span class="token keyword">return</span> GrayForcast<span class="token punctuation">(</span>series<span class="token operator">=</span>target_col<span class="token punctuation">)</span>

    @staticmethod
    <span class="token keyword">def</span> <span class="token function">build_from_excel</span><span class="token punctuation">(</span>csv_path<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>str<span class="token punctuation">,</span> Path<span class="token punctuation">]</span><span class="token punctuation">,</span> target_col<span class="token punctuation">:</span> Union<span class="token punctuation">[</span>None<span class="token punctuation">,</span> int<span class="token punctuation">,</span> str<span class="token punctuation">]</span><span class="token operator">=</span>None<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token string">"GrayForcast"</span><span class="token punctuation">:</span>
        csv_path <span class="token operator">=</span> csv_path <span class="token keyword">if</span> isinstance<span class="token punctuation">(</span>csv_path<span class="token punctuation">,</span> Path<span class="token punctuation">)</span> <span class="token keyword">else</span> Path<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>parent<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>csv_path<span class="token punctuation">)</span>
        df <span class="token punctuation">:</span> pd<span class="token punctuation">.</span>DataFrame <span class="token operator">=</span> pd<span class="token punctuation">.</span>read_excel<span class="token punctuation">(</span>csv_path<span class="token punctuation">,</span> header<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> index_col<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>

        <span class="token keyword">assert</span> <span class="token punctuation">(</span>multiple<span class="token punctuation">:</span><span class="token operator">=</span>df<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">and</span> target_col <span class="token keyword">is</span> None<span class="token punctuation">,</span> f<span class="token string">"xlsx文件存在多个序列：{df.columns}，请指定target_col参数指定需要预测的序列"</span>

        <span class="token keyword">if</span> multiple<span class="token punctuation">:</span>
            <span class="token keyword">if</span> isinstance<span class="token punctuation">(</span>target_col<span class="token punctuation">,</span> str<span class="token punctuation">)</span><span class="token punctuation">:</span>
                target_col <span class="token operator">=</span> df<span class="token punctuation">.</span>loc<span class="token punctuation">[</span>target_col<span class="token punctuation">]</span>
            <span class="token keyword">elif</span> isinstance<span class="token punctuation">(</span>target_col<span class="token punctuation">,</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
                target_col <span class="token operator">=</span> df<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> target_col<span class="token punctuation">]</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            target_col <span class="token operator">=</span> df
        <span class="token keyword">return</span> GrayForcast<span class="token punctuation">(</span>series<span class="token operator">=</span>target_col<span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    gf <span class="token operator">=</span> GrayForcast<span class="token punctuation">.</span>build_from_csv<span class="token punctuation">(</span>csv_path<span class="token operator">=</span><span class="token string">"./test2.csv"</span><span class="token punctuation">,</span> target_col<span class="token operator">=</span><span class="token string">"噪声"</span><span class="token punctuation">)</span>
    gf<span class="token punctuation">.</span>train<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>gf<span class="token punctuation">.</span>predict<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="B-使用说明"><a href="#B-使用说明" class="headerlink" title="B. 使用说明"></a>B. 使用说明</h3><p>代码从指定的表格中读取数据，然后先训练得到参数，最后通过预测指定，需要预测的项数即可</p>
<p>表格要求如下：第一列为序号，其余列为对比数列，对比数列沿列展开，若表格有多列则需要指定计算的列的名称</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107143018846.png" alt="示例表格"></p>
<p>使用结果如下</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107143325969.png" alt="计算结果"></p>
<h2 id="8-灰色预测案例"><a href="#8-灰色预测案例" class="headerlink" title="8. 灰色预测案例"></a>8. 灰色预测案例</h2><p>灰色预测模型处理的问题基本都是预测模型</p>
<h3 id="A-投资理财问题"><a href="#A-投资理财问题" class="headerlink" title="A. 投资理财问题"></a>A. 投资理财问题</h3><blockquote>
<p>银行有各种投资理财产品，客户可根据自己的资金实力和 投资偏好来自由选择，并且一般会有“10天犹豫期”，在 这10天里如果对自己购得的理财产品不放心或者不满意通 常情况下是可以退买的，这时候是不收手续费的。否则逾 期退买将收取一定的手续费。 通过对客户退买行为数据的分析，发现客户购得理财产品 后的每一天继续持有的客户比例依次是[92.810 97.660 98.800 99.281 99.537 99.537 99.817 0.00](单位%)，从这组数列可以看出退买高发期是在前几天，后续退买的可能性持续衰减。建立$GM(1,1)$模型对以上数据进行分析。</p>
</blockquote>
<h3 id="B-道路噪声平均值"><a href="#B-道路噪声平均值" class="headerlink" title="B. 道路噪声平均值"></a>B. 道路噪声平均值</h3><blockquote>
<p>北方某城市1986～1992 年道路交通噪声平均声级数据见表6，单位为db(A)</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220106225804772.png" alt="表6" style="zoom:50%;"></p>
</blockquote>
<p>利用上面的代码，准备好表格数据，然后开始预测</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107143325969.png" alt="道路噪声平均值"></p>
<h3 id="C-公司利润预测"><a href="#C-公司利润预测" class="headerlink" title="C. 公司利润预测"></a>C. 公司利润预测</h3><blockquote>
<p>已知某公司1999-2008年的利润为(单位：元/年)： [89677,99215,109655,120333,135823,159878,182321,209407, 246619,300670],现在要预测该公司未来几年的利润情况。</p>
</blockquote>
<p>把上面的数据制作成下面的表格</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107144656000.png" alt="公司利润表" style="zoom:50%;"></p>
<p>得到未来五年利润的预测如下</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107144924383.png" alt="未来五年的利润预测"></p>
<h3 id="D-SARS病毒影响"><a href="#D-SARS病毒影响" class="headerlink" title="D. SARS病毒影响"></a>D. SARS病毒影响</h3><blockquote>
<p>2003年的SARS疫情对中国部分行业的经济发展产生了一 定的影响，特别是对帮分疫情较严重的省市的相关行业所造成的影响是明显的，经济影响主要分为直接经济影响和 间接影响。直接经济影响涉及到商品零售业、旅游业、综合服务等行业。很多方面难以进行定量地评估，现仅就 SARS疫情较重的某市商品零售业、旅游业和综合服务业的影响进行定量的评估分析。</p>
<p>究竟SARS疫情对商品零售业、旅游业和综合服务业的影 响有多大，已知该市从1997年1月到2003年10月的商品零 售额、接待旅游人数和综合服务收入的统计数据如下表1 、表2、表3．</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107150033656.png" alt="表1，单位：亿元" style="zoom:67%;"></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107150057008.png" alt="表2，单位：亿元" style="zoom: 67%;"></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20220107150130466.png" alt="表三，单位：亿元" style="zoom:67%;"></p>
<p>试根据这些历史数据建立预测评估模型，评估2003年SARS疫 情给该市的商品零售业、旅游业和综合服务业所造成的影响。</p>
</blockquote>
<p>对SARS病毒影响的评估，那么可以用没有疫情和有疫情的对比实现。例如要探究SARS对零售业的影响，那么其实可以首先预测出来没有SARS零售业本来的指标，然后和有SARS的指标进行对比，从而给出对SARS病毒的影响。</p>
<p>因此思路如下：</p>
<p>根据所掌握的历史统计数据可以看出，在正常情况下，全 年的平均值较好地反映了相关指标的变化规律，这样可以 把预测评估分成两部分：</p>
<ol>
<li>利用灰色理论建立灰微分方程模型，由1997~2002年的 平均值预测2003年平均值</li>
<li>通过历史数据计算每个月的指标值与全年总值的关系 ，从而可预测出正常情况下2003年每个月的指标值，再与 实际值比较可以估算出SARS疫情实际造成的影响</li>
</ol>
<p>具体求解略</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack Wang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jackwang0107.github.io/2022/01/06/shu-xue-jian-mo-suan-fa-6-hui-se-guan-lian-fen-xi-yu-yu-ce/">https://jackwang0107.github.io/2022/01/06/shu-xue-jian-mo-suan-fa-6-hui-se-guan-lian-fen-xi-yu-yu-ce/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack Wang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Python/">
                                    <span class="chip bg-color">Python</span>
                                </a>
                            
                                <a href="/tags/Math-Modeling/">
                                    <span class="chip bg-color">Math Modeling</span>
                                </a>
                            
                                <a href="/tags/%E7%81%B0%E8%89%B2%E5%85%B3%E8%81%94%E5%88%86%E6%9E%90/">
                                    <span class="chip bg-color">灰色关联分析</span>
                                </a>
                            
                                <a href="/tags/%E7%81%B0%E8%89%B2%E9%A2%84%E6%B5%8B/">
                                    <span class="chip bg-color">灰色预测</span>
                                </a>
                            
                                <a href="/tags/%E7%81%B0%E8%89%B2%E6%A8%A1%E5%9E%8B/">
                                    <span class="chip bg-color">灰色模型</span>
                                </a>
                            
                                <a href="/tags/Grey-relational-analysis/">
                                    <span class="chip bg-color">Grey relational analysis</span>
                                </a>
                            
                                <a href="/tags/Grey-Model/">
                                    <span class="chip bg-color">Grey Model</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/01/07/shu-xue-jian-mo-suan-fa-7-yi-wei-er-wei-cha-zhi-yi-ji-ni-he/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/300px-Splined_epitrochoid.svg.png" class="responsive-img" alt="数学建模算法7-一维二维插值以及拟合">
                        
                        <span class="card-title">数学建模算法7-一维二维插值以及拟合</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文主要介绍了数学建模中常见的算法（数据处理方法）：插值以及拟合
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-01-07
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E7%AE%97%E6%B3%95/" class="post-category">
                                    数学建模算法
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Python/">
                        <span class="chip bg-color">Python</span>
                    </a>
                    
                    <a href="/tags/Math-Modeling/">
                        <span class="chip bg-color">Math Modeling</span>
                    </a>
                    
                    <a href="/tags/%E4%B8%80%E7%BB%B4%E4%BA%8C%E7%BB%B4%E6%8F%92%E5%80%BC/">
                        <span class="chip bg-color">一维二维插值</span>
                    </a>
                    
                    <a href="/tags/%E6%8B%9F%E5%90%88/">
                        <span class="chip bg-color">拟合</span>
                    </a>
                    
                    <a href="/tags/Interpolation/">
                        <span class="chip bg-color">Interpolation</span>
                    </a>
                    
                    <a href="/tags/Fitting/">
                        <span class="chip bg-color">Fitting</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/01/04/shu-xue-jian-mo-suan-fa-5-ceng-ci-fen-xi-fa/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/v2-986c79a5dfbf54baa7dbe8c29d5f6734_1440w.jpg" class="responsive-img" alt="数学建模算法5-层次分析法">
                        
                        <span class="card-title">数学建模算法5-层次分析法</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文主要介绍了数学建模中常见的评价方法：层次分析法
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-01-04
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E7%AE%97%E6%B3%95/" class="post-category">
                                    数学建模算法
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Python/">
                        <span class="chip bg-color">Python</span>
                    </a>
                    
                    <a href="/tags/Math-Modeling/">
                        <span class="chip bg-color">Math Modeling</span>
                    </a>
                    
                    <a href="/tags/%E5%B1%82%E6%AC%A1%E5%88%86%E6%9E%90%E6%B3%95/">
                        <span class="chip bg-color">层次分析法</span>
                    </a>
                    
                    <a href="/tags/Analytic-Hierarchy-Process/">
                        <span class="chip bg-color">Analytic Hierarchy Process</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: JackWang&#39;s Blog<br />'
            + '文章作者: Jack Wang<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>


<script src="https://cdn.bootcss.com/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
    MathJax.Hub.Config({
        tex2jax: {inlineMath: [['$', '$'], ['\\(', '\\)']]}
    });
</script>



    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <a href="/about" target="_blank">Jack Wang</a>
            <!-- |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a> -->
            <!-- |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">603.8k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "11";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp"><img src="/medias/icp.png"
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">陕ICP备2021014294号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jackwang0108" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2232123545@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2232123545" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2232123545" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
